<?php
/**
 * 回调——控制器
 * @property WxPayApi $wxPayApi
 * @property WxCallback	$wxCallback
 * @property Recharge_order_mdl $rechargeOrder
 * @property User_mdl $user
 */
class Callback extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->library('wxApi/WxPayApi',NULL,'wxPayApi');
		$this->load->model('Recharge_order_mdl','rechargeOrder');
		$this->load->model('User_mdl','user');
		$this->apiOutput->debug = true;
	}
	
	var $originalXML = NULL;

	/**
	 * 接收微信支付的支付结果(由微信支付系统在用户确认支付后异步调用)
	 */
	public function  wx_pay(){
// 成功时返回数据:
	// <xml><appid><![CDATA[wx0f145ccb4139afdf]]></appid>
	// 	<bank_type><![CDATA[CFT]]></bank_type>
	// 	<cash_fee><![CDATA[1]]></cash_fee>
	// 	<fee_type><![CDATA[CNY]]></fee_type>
	// 	<is_subscribe><![CDATA[N]]></is_subscribe>
	// 	<mch_id><![CDATA[1377381802]]></mch_id>
	// 	<nonce_str><![CDATA[57ad76fd2bde4]]></nonce_str>
	// 	<openid><![CDATA[oaEq4vy2HNU8haXlWpvCa7uKoxi8]]></openid>
	// 	<out_trade_no><![CDATA[147209642700000030]]></out_trade_no>
	// 	<result_code><![CDATA[SUCCESS]]></result_code>
	// 	<return_code><![CDATA[SUCCESS]]></return_code>
	// 	<sign><![CDATA[48E4F0A1714933BCF4639DE3BC28E873]]></sign>
	// 	<time_end><![CDATA[20160812151315]]></time_end>
	// 	<total_fee>1</total_fee>
	// 	<trade_type><![CDATA[APP]]></trade_type>
	// 	<transaction_id><![CDATA[4006182001201608121173518907]]></transaction_id>
	// </xml>
		// 获取并处理回调内容
		$result = $this->wxPayApi->notifyPayResult();
		// 获取并记录微信回调的原始数据
		$this->originalXML = $this->wxPayApi->getOriginalRespose();
		
		if ( ! $result ) {
			$errors = $this->wxPayApi->getErrors();
			$this->_exit('FAIL', $errors['msg']);
		}
		// 业务结果为失败时的处理
		if( empty($result['result_code']) || $result['result_code']!='SUCCESS' ){
			
		}
		if( empty($result['out_trade_no']) ){
			$this->_exit('FAIL','无商户订单号');
		}
		if( empty($result['transaction_id']) ){
			$this->_exit('FAIL','无微信支付订单号');
		}
		
		$where['tradeID'] = $result['out_trade_no'];
		// 从数据库查该订单状态
		$order = $this->rechargeOrder->getOne($where);
		
		if (empty($order)){
			$this->_exit('FAIL','无对应订单');
		}
// 		$result = $this->rechargeOrder->queryOrderResult($order);
// 		if ( $result['tradeStateChanged'] && $result['tradeState']=='SUCCESS' ) {
// 			$user = $this->user->findOneByID($order['userID']);
// 			$user->balance+=$order['amount'];
// 			$user->update();
// 		}

		$this->_exit('SUCCESS', 'OK');
	}
	
	private function _exit($return_code,$return_msg){
		$this->_log();
		echo '<xml>
				<return_code><![CDATA['.$return_code.']]></return_code>
				<return_msg><![CDATA['.$return_msg.']]></return_msg>
			</xml>';
		exit();
	}
	
	private function _log(){
		static $filename = NULL;
		if ( NULL === $filename ) {
			$dir = APPPATH.'logs/wxCallbackXML/';
			if( !is_dir($dir) ){
				mkdir($dir);
			}
			$dir.=date('y_m_d').'/';
			if( !is_dir($dir) ){
				mkdir($dir);
			}
			do{
				$filename = $dir.date('H_i_s').'_'.mt_rand(0, 9999);
			}while(file_exists($filename));
		}
		$debug = MyDebug::$data;
		$debug = array2XmlString($debug,'debug');
		$xml = $this->originalXML;
		$ext = substr($xml, -6);
		if ( strtolower($ext) == '</xml>' ) {
			$xml = str_replace($ext, $debug.$ext, $xml);
		}else{
			$xml.=$debug;
		}
		file_put_contents($filename, $xml);
	}
	
	
}







